package com.lgx.right.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.right.dao.IOrganizationDao;
import com.lgx.right.dao.IUserDao;
import com.lgx.right.entity.Organization;
import com.lgx.right.entity.User;
import com.lgx.right.service.IOrganizationService;

@Service("organizationService")
public class OrganizationService implements IOrganizationService {

	@Autowired
	private IOrganizationDao organizationDao;
	
	@Autowired
	private IUserDao userDao;
	
	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IOrganizationDao getOrganizationDao() {
		return organizationDao;
	}

	public void setOrganizationDao(IOrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

	@Transactional
	public Organization createOrganization(Organization o) throws DataException {
		return this.organizationDao.createOrganization(o);
	}

	@Transactional
	public Organization updateOrganization(Organization o) throws DataException {
		return this.organizationDao.updateOrganization(o);
	}

	@Transactional
	public void deleteOrganization(Organization o) throws DataException {
		this.organizationDao.deleteOrganization(o);
	}

	@Transactional
	public void deleteOrganization(long id) throws DataException {
		this.organizationDao.deleteOrganization(id);
	}

	public Organization findOrganizationById(long id) {
		return this.organizationDao.findOrganizationById(id);
	}
	
	public Organization findOrganizationByUserId(long userId){
		return this.organizationDao.findOrganizationByUserId(userId);
	}

	public List<Organization> findOrganizationList(String sql) {
		return this.organizationDao.findOrganizationList(sql);
	}
	
	public List<Organization> findOrganizationListByareaNode(String areaNode) {
		return this.organizationDao.findOrganizationListByareaNode(areaNode);
	}

	public List<Organization> findOrganizationList(String sql, Object[] params,
			int pageSize, int pageNum) {
		return this.organizationDao.findOrganizationList(sql, params, pageSize, pageNum);
	}

	public Pagination findOrganizationPagination(String sql, Object[] params,
			int pageSize, int pageNum) {
		return this.organizationDao.findOrganizationPagination(sql, params, pageSize, pageNum);
	}

	
	public Pagination findOrganizationPagination(Object[] params, int pageSize,
			int pageNum) {
		return this.organizationDao.findOrganizationPagination(params, pageSize, pageNum);
	}

	public List<Organization> findOrganizationParent(String type) {
		return this.organizationDao.findOrganizationParent(type);
	}
	public void deleteOrganizations(String[] ids) throws DataException {
		
		for(String id : ids)
		{
			Organization org = this.findOrganizationById(Long.parseLong(id));
			User legal = org.getLegal();
			legal.setIsMaster("1");
			legal.setIsDelete("1");
			this.userDao.updateUser(legal);
			
			org.setIsDelete("1");
			this.organizationDao.updateOrganization(org);
		}
	}
	
	public List<Organization> findOrganizationListByPoints(Double x,Double y){
		return this.organizationDao.findOrganizationListByPoints(x,y);
	}
}
